package casper.alpha.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBConnection {
	
	private static String DB_DRIVER = "org.postgresql.Driver";
	private static String DB_CONNECTION_VRP = "jdbc:postgresql://localhost:5432/master";
	private static String DB_CONNECTION_VRP152 = "jdbc:postgresql://localhost:5432/master152";
	private static String DB_CONNECTION_VRP33 = "jdbc:postgresql://localhost:5432/master33";
	
	private static String DB_CONNECTION_E0 = "jdbc:postgresql://localhost:5432/mastere0";
	private static String DB_CONNECTION_E1 = "jdbc:postgresql://localhost:5432/mastere1";
	private static String DB_CONNECTION_E2 = "jdbc:postgresql://localhost:5432/mastere2";
	private static String DB_CONNECTION_E3 = "jdbc:postgresql://localhost:5432/mastere3";
	private static String DB_CONNECTION_E4 = "jdbc:postgresql://localhost:5432/mastere4";
	private static String DB_CONNECTION_E5 = "jdbc:postgresql://localhost:5432/mastere5";
	
	private static String DB_CONNECTION = DB_CONNECTION_E5;
	private static String DB_USER = "postgres";
	private static String DB_PASSWORD = "daisyblue";
	
	public static void setDB(int db) {
		switch(db) {
		case 33:
			DB_CONNECTION = DB_CONNECTION_VRP33;
			break;
		case 152:
			DB_CONNECTION = DB_CONNECTION_VRP152;
			break;
		}
	}
	
	public static void execute(String query) throws SQLException {
		 
		Connection dbConnection = null;
		Statement statement = null;
 
		try {
			dbConnection = getDBConnection();
			statement = dbConnection.createStatement();
 
			// System.out.println(query);
 
			// execute update SQL stetement
			statement.execute(query);
 
		} catch (SQLException e) {
 
			System.out.println(e.getMessage());
 
		} finally {
			if (statement != null) {
				statement.close();
			}
			/*
			if (dbConnection != null) {
				dbConnection.close();
			}
 			*/
		}
 
	}
 
	public static ResultSet executeQuery(String query) throws SQLException {
 
		Connection dbConnection = null;
		Statement statement = null;
		ResultSet result = null;
		try {
			dbConnection = getDBConnection();
			statement = dbConnection.createStatement();
 
			// System.out.println(query);
            // execute the SQL stetement
			result = statement.executeQuery(query);
 
		} catch (SQLException e) {
 
			System.out.println(e.getMessage());
 
		} finally {
			/*
			if (statement != null) {
				statement.close();
			}
			*/
			/*
			if (dbConnection != null) {
				dbConnection.close();
			}
			 */
		}
		
		return result;
 
	}
	
	private static Connection dbConnection = null;
	private static Connection dbConnection1 = null;
	private static Connection dbConnection2 = null;
	private static Connection dbConnection3 = null;
	private static Connection dbConnection4 = null;
	private static Connection dbConnection5 = null;
 
	private static Connection getDBConnection() {
		
		if(dbConnection != null) {
			return dbConnection;
		}
		
		try {
 
			Class.forName(DB_DRIVER);
 
		} catch (ClassNotFoundException e) {
 
			System.out.println(e.getMessage());
 
		}
 
		try {
 
			dbConnection = DriverManager.getConnection(
					DB_CONNECTION, DB_USER,DB_PASSWORD);
			return dbConnection;
 
		} catch (SQLException e) {
 
			System.out.println(e.getMessage());
 
		}
 
		return dbConnection;
 
	}
}
